<?php
/**
 * Description of IndexController
 *
 * @author Admin
 */
class Main_ProfileController extends Zend_Controller_Action
{
    protected $_model;
    protected $_authService;

    public function init()
    {
        $layout = $this->_helper->layout();
        $layout->setLayout('dashboard');

        $this->_authService = new Main_Service_Authentication();
        $this->_model   =   new Main_Model_User();

        $this->view->headLink()->appendStylesheet('/css/validationEngine.css');

        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.ve.js', 'text/javascript' );
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.ve-en.js', 'text/javascript' );
    }

    public function indexAction()
    {
        if(!$this->_authService->getAuth()->getIdentity())
                 $this->_redirect('/login');
        
        $this->view->currentLoggedinUser = $this->_authService->getCurrentUserName();
    }

    public function profileAction()
    {
        if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');

        $addressModel = new Main_Model_Address();
        $userData = $this->_authService->getAuth()->getStorage()->read();
        $this->view->userData = $userData;
        $this->view->CorAdd = $addressModel->getUserCorrespondanceAddress($userData->uid);
        $this->view->EmerAdd = $addressModel->getUserEmergencyAddress($userData->uid);
        
        //$this->view->CorAdd = '';
        //$this->view->EmerAdd = '';
        
        $this->view->Addresses = $addressModel->getUserAddress($userData->uid);
        
    }

    public function editAction()
    {
        if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');
        
        $this->view->userData = $this->_authService->getAuth()->getStorage()->read();
        $this->view->userData = $this->_model->getUserById($this->view->userData->uid);
    }

    public function editpostAction()
    {
        /*
        echo '<pre>';
        print_r($this->getRequest()->getParams());
        echo '</pre>';
        exit;
        */
        if (false === $this->_model->saveUser($this->getRequest()->getPost())  )
        {
            $this->_helper->FlashMessenger('')->setNamespace('error')->addMessage('An error occured while saving profile data');

            return $this->render('/edit-profile');
        }

        $this->_helper->FlashMessenger('')->setNamespace('success')->addMessage('Profile info saved');
        return $this->_redirect('/profile');
    }

    public function addressAction()
    {
        $addressModel = new Main_Model_Address();
        $userData = $this->_authService->getAuth()->getStorage()->read();
        //echo $userData->uid;die;
        $this->view->Addresses = $addressModel->getUserAddress($userData->uid);
    }


    public function newaddressAction()
    {
         if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');
    }
    
    public function editaddressAction()
    {
         if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');
         
         $userData = $this->_authService->getAuth()->getStorage()->read();
         $userid = $userData->uid;

         $addressModel = new Main_Model_Address();
         $this->view->UserAdd = $addressModel->getUserAddress($userid)->toArray();
         
    }

    public function addresspostAction()
    {
         if(!$this->_authService->getAuth()->getIdentity())
             $this->_redirect('/login');
         
         $userData = $this->_authService->getAuth()->getStorage()->read();
         $userid = $userData->uid;
         //echo '<pre>';
         //print_r($this->getRequest()->getPost());
         //die;
         $addressData = $this->getRequest()->getPost();
         $addressData['uid'] = $userid;
         $addressModel = new Main_Model_Address();
         //$addressModel->createAddress($addressData);
         if($addressModel->createAddress($addressData)){
             if(isset($addressData['hiddenAddressId'])){
                 $this->_helper->FlashMessenger('')->addMessage('Address has been updated successfully!');
             }else{
                 $this->_helper->FlashMessenger('')->addMessage('Address has been added successfully!');
             }
         }else{
             $this->_helper->FlashMessenger('')->addMessage('Error ! Try again later.');
         }
         
         $this->_redirect('/profile');
    }

    public function changepasswordAction()
    {
         if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');
         
         $userData = $this->_authService->getAuth()->getStorage()->read();
         $userid = $userData->uid;
         $changeData = $this->getRequest()->getPost();
         $changeData['uid'] = $userid;
         
         if($changeData['Password'] == $changeData['ConfirmPassword']){
             $this->_model = new Main_Model_User();
             if($this->_model->changePassword($changeData)){
                 $this->_helper->FlashMessenger('')->addMessage('Password has been changed successfully!');
             }else{
                 $this->_helper->FlashMessenger('')->addMessage('Error ! Try again later.');
             }
         }else{
             $this->_helper->FlashMessenger('')->addMessage('Password and Confirm Password must be same');
         }
         $this->_redirect('/profile');
    }

    public function securityquestionAction()
    {
        if(!$this->_authService->getAuth()->getIdentity())
            $this->_redirect('/login');
    }
    
}
?>
